package cn.tjnext.apply.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Converter {

    /**
     * JSON字符串转成Date
     * @param jSONDate
     * @return
     */
    public static Date jSONToDate (String jSONDate) {
        SimpleDateFormat sdfJSON = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        try {
            date = sdfJSON.parse(jSONDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * Date转成JSON字符串
     * @param date
     * @return
     */
    public static String dateToJSON (Date date) {
        SimpleDateFormat sdfJSON = new SimpleDateFormat("yyyy-MM-dd");
        String jSONDate = sdfJSON.format(date);
        return jSONDate;
    }

    /**
     * url字符串转成Date
     * @param urlDate
     * @return
     */
    public static Date urlToDate (String urlDate) {
        SimpleDateFormat sdfUrl = new SimpleDateFormat("yyyyMMdd");
        Date date = new Date();
        try {
            date = sdfUrl.parse(urlDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * Date转成url字符串
     * @param date
     * @return
     */
    public static String dateToUrl (Date date) {
        SimpleDateFormat sdfJSON = new SimpleDateFormat("yyyyMMdd");
        String jSONDate = sdfJSON.format(date);
        return jSONDate;
    }

    /**
     * 获取年份
     * @param date
     * @return
     */
    public static int dateToInt (Date date) {
        SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy");
        return Integer.parseInt(sdfDate.format(date));
    }

}
